package com.health.work.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import com.health.work.R;
import com.health.work.activity.Products.ViewHolder;
import com.health.work.model.ProductsBean;
import com.health.work.model.TrusteeBean;
import com.health.work.utils.AsyncImageLoader;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyTrusteeShipActivity extends Activity {
	private Thread thread;
	private TextView back;
	private ListView listView;

	private String mobilePhoneNum;
	private String userPhoneNum;
	private String trusteeshipState;
	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private final static int NODATA = 3;
	private final static int SUCCESSC = 4;
	private final static int FAILEC = 5;
	private ArrayList<TrusteeBean> truList;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				setData();
				break;
			case FAILE:
				Toast.makeText(MyTrusteeShipActivity.this, "请求失败",
						Toast.LENGTH_SHORT).show();
				break;
			case ERROR:
				Toast.makeText(MyTrusteeShipActivity.this, "未知错误",
						Toast.LENGTH_SHORT).show();
				break;
			case NODATA:
				TipsUtil.showAlertFinish(MyTrusteeShipActivity.this, null,
						"暂无托管！");
				break;
			case SUCCESSC:
				Toast.makeText(MyTrusteeShipActivity.this, "操作成功",
						Toast.LENGTH_SHORT).show();
				break;
			case FAILEC:
				Toast.makeText(MyTrusteeShipActivity.this, "操作失败",
						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_trusteeship_layout);
		mobilePhoneNum = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		initPages();
		getData();
	}

	private void initPages() {
		// TODO Auto-generated method stub
		listView = (ListView) findViewById(R.id.pro_list);
		back = (Button) findViewById(R.id.btnBack);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
				userPhoneNum = truList.get(arg2).MemberMobilePhone;
				Intent intent;
				intent=new Intent(MyTrusteeShipActivity.this, Cases.class);
				intent.putExtra("mobileNum",userPhoneNum);
				startActivity(intent);
				// intent=new Intent(MyTrusteeShipActivity.this, uri)
			}
		});
	}

	private void getData() {
		// TODO Auto-generated method stub
		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().GetTrusteeshipByExpert(
						mobilePhoneNum, "", new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								if ("0".equals(result)) {
									handler.sendEmptyMessage(NODATA);
								} else if ("1".equals(result)) {
									handler.sendEmptyMessage(FAILE);
								} else {
									handler.sendEmptyMessage(ERROR);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub

							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub
								if (array.toString() != null
										&& array.length() > 0) {
									truList = new ArrayList<TrusteeBean>();
									for (int i = 0; i < array.length(); i++) {
										TrusteeBean bean = new TrusteeBean();
										bean.Commission = array
												.getJSONObject(i).getInt(
														"Commission");
										bean.ConfirmDate = array.getJSONObject(
												i).getString("ConfirmDate");
										bean.CreateDate = array
												.getJSONObject(i).getString(
														"CreateDate");
										bean.ExpertMobilePhone = array
												.getJSONObject(i).getString(
														"ExpertMobilePhone");
										bean.Id = array.getJSONObject(i)
												.getInt("Id");
										bean.IsPay = array.getJSONObject(i)
												.getString("IsPay");
										bean.MemberMobilePhone = array
												.getJSONObject(i).getString(
														"MemberMobilePhone");
										bean.PayMoney = array.getJSONObject(i)
												.getInt("PayMoney");
										bean.PayTime = array.getJSONObject(i)
												.getString("PayTime");
										bean.State = array.getJSONObject(i)
												.getString("State");
										bean.SumMoney = array.getJSONObject(i)
												.getString("SumMoney");
										truList.add(bean);
									}
									handler.sendEmptyMessage(SUCCESS);
								}
							}
						});
			}
		});
		thread.start();
	}

	/**
	 * 托管确认
	 */

	private void TrusteeShip() {

		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().ConfirmTrusteeship(userPhoneNum,
						mobilePhoneNum, trusteeshipState,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								if ("0".equals(result)) {
									handler.sendEmptyMessage(SUCCESSC);
								} else if ("1".equals(result)) {
									handler.sendEmptyMessage(FAILEC);
								} else {
									handler.sendEmptyMessage(ERROR);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub

							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub

							}
						});
			}
		});
		thread.start();
	}

	private void setData() {

		TruAdapter adapter = new TruAdapter(MyTrusteeShipActivity.this);
		listView.setAdapter(adapter);
	}

	private class TruAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private AsyncImageLoader asyncImageLoader;
		Activity context;

		public TruAdapter(Context context) {
			this.context = (Activity) context;
			asyncImageLoader = new AsyncImageLoader();
			inflater = this.context.getLayoutInflater();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return truList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return truList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder viewHodler;
			if (convertView == null) {
				viewHodler = new ViewHolder();
				convertView = inflater.inflate(R.layout.trusteeship_item,
						parent, false);
				viewHodler.money = (TextView) convertView
						.findViewById(R.id.money);
				viewHodler.state = (TextView) convertView
						.findViewById(R.id.state);
				viewHodler.pay = (TextView) convertView.findViewById(R.id.pay);
				viewHodler.line = (LinearLayout) convertView
						.findViewById(R.id.linear_bottom);
				viewHodler.btn1 = (Button) convertView
						.findViewById(R.id.submit);
				viewHodler.btn2 = (Button) convertView
						.findViewById(R.id.problemlist);
			} else {
				viewHodler = (ViewHolder) convertView.getTag();
			}
			final TrusteeBean bean = truList.get(position);
			viewHodler.money.setText("总金额：" + bean.SumMoney);
			if("false".equals(bean.IsPay)){
				viewHodler.pay.setText("未付款");
			}else if("true".equals(bean.IsPay)){
				viewHodler.pay.setText("已付款");
			}
			
			viewHodler.state.setText(bean.State);
			viewHodler.btn1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					userPhoneNum = bean.MemberMobilePhone;
					trusteeshipState = "确认";
					TrusteeShip();
				}
			});
			viewHodler.btn2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					userPhoneNum = bean.MemberMobilePhone;
					trusteeshipState = "关闭";
					TrusteeShip();
				}
			});
			convertView.setTag(viewHodler);
			return convertView;
		}

	}

	class ViewHolder {
		public TextView money;
		public TextView state;
		public TextView pay;
		public LinearLayout line;
		public Button btn1;
		public Button btn2;
	}
}
